<?php
namespace Admin\Controller;
use Common\Controller\AdminbaseController;
/*成员管理*/
class AppUserController extends AdminbaseController{
	protected $model_model;
	protected $group_model;
	protected $user_group;
	protected $constants;
	function _initialize() {
		parent::_initialize();
		$this->model_model = D("Common/AppUser");
		$this->group_model = D("Common/AppGroup");
		$this->user_group=D("Common/UserGroup");
		$this->constants=D("Common/Constant");
	}
	
	function index(){
		$this->_list();
		$this->display();
	}
	
	function add(){
		$this->display();
	}
	
	function add_post(){
		if(IS_POST){
			$is_user=$this->model_model->where("user_uniq_id='".I("user_uniq_id",'')."'")->count(1);
			$user_account=$this->model_model->where("user_account='".I("user_account",'')."'")->count(1);
			if($is_user>0){
				$this->error("互动id 已经存在！");
			}
			if($user_account>0){
				$this->error("此手机号码被使用！");
			}
			$data=$this->model_model->create();
			if ($data){
				/*群数据*/
				$option['username']=$data['user_uniq_id'];
				$option['password']=$data['user_uniq_id'];
				$option['nickname']=$data['nick'];
				$hx_result=$this->accreditRegister($option);
				\Think\Log::write("注册环信账号  accreditRegister--- 环信代码--".json_encode($hx_result));
				if(empty($hx_result['error'])){// 环信接口访问正常
					$this->model_model->add($data);
				    $this->success("成功",U("AppUser/index"));
				}else{// 环信接口访问出错
				   $this->error(L('ADD_FAILED'));
				}
				
			} else {
				$this->error($this->model_model->getError());
			}
		
		}else{
			 $this->error(L('ADD_FAILED'));
		}
	}
	
	function edit(){
		$id=I("get.id");
		$model=$this->model_model->where("id=$id")->find();
		$this->assign('model',$model);
		$this->assign('group_list',$this->group_model->my_group_list($model['user_uniq_id']));
		$this->display();
	}
	
	function edit_post(){
		if (IS_POST) {
			if ($this->model_model->create()) {
				if ($this->model_model->save()!==false) {
					$this->success("保存成功！", U("ad/index"));
				} else {
					$this->error("保存失败！");
				}
			} else {
				$this->error($this->model_model->getError());
			}
		}
	}
	
	/**
	 *  删除
	 */
	function delete(){
		$id = I("get.id",0,"intval");
		if ($this->model_model->delete($id)!==false) {
			$this->success("删除成功！");
		} else {
			$this->error("删除失败！");
		}
	}
	
	function toggle(){
		if(isset($_POST['ids']) && $_GET["display"]){
			$ids = implode(",", $_POST['ids']);
			$data['status']=1;
			if ($this->model_model->where("id in ($ids)")->save($data)!==false) {
				$this->success("显示成功！");
			} else {
				$this->error("显示失败！");
			}
		}
		if(isset($_POST['ids']) && $_GET["hide"]){
			$ids = implode(",", $_POST['ids']);
			$data['status']=0;
			if ($this->model_model->where("id in ($ids)")->save($data)!==false) {
				$this->success("隐藏成功！");
			} else {
				$this->error("隐藏失败！");
			}
		}
	}
	///////////////////////////////////////////////////////////////////////////
		private function _list() {
	    $fields = array('grade'=>array("field" =>"grade","operator" =>"like"),
						'user_uniq_id'=>array("field" =>"user_uniq_id","operator" =>"like"),
						'nick'=>array("field" =>"nick","operator" =>"like"),
						'user_account'=>array("field" =>"user_account","operator" =>"="),
						);
	    $where_and = $this ->get_where_ands(IS_POST, $fields);
	    $where = join(' and ', $where_and);
	    $count = $this ->model_model -> where($where) ->count(1);
	
	    $page = $this ->page($count, 20);
	    $this ->assign("Page", $page ->show('Admin'));
	    $this ->assign("current_page", $page ->GetCurrentPage());
	    $list = $this ->model_model ->field(true)->where($where) ->order('create_time desc') ->limit($page ->firstRow.','.$page ->listRows) ->select();
		
	    $this ->assign('list', $list);
		$this->assign("grades",$this->constants->getGrades());// 获取会员等级列表
	}
	
	///////////////////////////////////////////////////////////////////////
}